package com.issac.common;


import java.io.Serializable;

/**
 * @ClassName R
 * @Author zy
 * @Date 2021/10/26 22:35
 * @Description 结果集
 * @Version 1.0
 */
public class R implements Serializable {

    // --------------- static fields ------------------

    public static final int DEFAULT_SUCCESS_CODE = 200;
    public static final int DEFAULT_ERROR_CODE = 500;

    public static final String DEFAULT_SUCCESS_MSG = "操作成功";
    public static final String DEFAULT_ERROR_MSG = "操作失败";

    // --------------- fields ------------------

    private String msg;

    private int code;

    private Object data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
// --------------- success method ------------------

    public static <T> R ok () {
        return ok(DEFAULT_SUCCESS_MSG, DEFAULT_SUCCESS_CODE, null);
    }

    public static <T> R ok (T data) {
        return ok(DEFAULT_SUCCESS_MSG, DEFAULT_SUCCESS_CODE, data);
    }

    public static <T> R ok (String msg, int code, T data) {
        R r = new R();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }

    // --------------- error method ------------------

    public static <T> R error () {
        return error(DEFAULT_ERROR_MSG, DEFAULT_ERROR_CODE, null);
    }

    public static <T> R error (T data) {
        return error(DEFAULT_ERROR_MSG, DEFAULT_ERROR_CODE, data);
    }

    public static <T> R error (String msg) {
        return error(msg, DEFAULT_ERROR_CODE, null);
    }

    public static <T> R error (String msg, int code, T data) {
        R r = new R();
        r.setMsg(msg);
        r.setData(data);
        r.setCode(code);
        return r;
    }
}

